package cat.music.ut.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import cat.music.ut.base.BaseRvAdapter
import cat.music.ut.base.CommonRvHolder
import cat.music.ut.bean.SearchResult
import cat.music.ut.component.click
import cat.music.ut.component.dp2px
import cat.music.ut.databinding.ItemSearchBinding
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions

class SearchResultAdapter(context: Context, data: MutableList<SearchResult>) :
    BaseRvAdapter<SearchResult, ItemSearchBinding>(context, data) {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): CommonRvHolder<ItemSearchBinding> {
       return  CommonRvHolder(
            ItemSearchBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(
        holder: CommonRvHolder<ItemSearchBinding>,
        position: Int,
        vb: ItemSearchBinding,
        bean: SearchResult
    ) {
        vb.ivTitle.text = bean.songName
        vb.tvDesc.text = "${bean.artist} - ${bean.album}"

        Glide.with(context)
            .load(bean.pic)
            .apply(RequestOptions.bitmapTransform(RoundedCorners(dp2px(context,25))))  // 设置圆角半径为30
            .into(vb.ivHead)
        vb.tvDownload.click {
            listener?.clickItem(bean)
        }
    }
    var listener:Listener? = null
    interface Listener {
        fun clickItem(data: SearchResult)
    }
}